<?php
abstract class Node_content extends Module {
    private $node_type = '';
    
    public function set_node_type ($node_type) {
        $this->node_type = $node_type;
    }
    
    public function act_add() {
        $node_module = m('node');
        $node_type = trim($this->node_type);
        if (empty($node_type)) tos_die();
            
        if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
            $node_id = (int) $node_module->add_node_data($node_type);
            if ($node_id <= 0) tos_die();
            $content_id = (int) $this->edit_content($node_id);
            if ($content_id <= 0) {
                tos_die();
            }
            $submit = tos_http_post('submit');
            if ($submit == 'Save') {
                tos_router_redirect(array('m' => $node_type, 'a' => 'edit', 'node_id' => $node_id));
            } else {
                tos_router_redirect(array('m' => 'node', 'a' => 'display', 'node_id' => $node_id));
            }
        }
        
        $out = out();
        $uc_type = ucfirst($node_type);
        $node_block_add = $node_module->block_add(
            array(
                'page_caption' => 'Add ' . $uc_type,
                'm' => $node_type,
                'a' => 'add'
            ));
        $content_block_add = $this->view("node/{$node_type}_edit");
        $node_block_add->add_block($content_block_add);
        
        $out->add_title('Add ' . $uc_type);
        $out->add_block($node_block_add);
    }
    
    public function act_edit() {
        $module_node = m('node');
        $node_id = (int) tos_http_get('node_id');
        if ($node_id <= 0) tos_die();
        
        $node_type = trim($this->node_type);
        if (empty($node_type)) tos_die();
        
        if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
            $module_node->edit_node_data($node_id);
            $this->edit_content($node_id);
            
            $submit = tos_http_post('submit');
            if ($submit == 'Save') {
                tos_router_redirect(array('m' => $node_type, 'a' => 'edit', 'node_id' => $node_id));
            } else {
                tos_router_redirect(array('m' => 'node', 'a' => 'display', 'node_id' => $node_id));
            }
        }
        
        $out = out();
        $out->add_title('Edit ' . $node_type);
        $block_node_edit = $module_node->block_edit(
            array(
                'page_caption' => 'Edit ' . $node_type,
                'm' => $node_type,
                'a' => 'edit',
                'node_id' => $node_id
        ));
        $content = $this->model($node_type)->get_content($node_id);
        $block_content_edit = $this->view("node/{$node_type}_edit", $content);
        $block_node_edit->add_block($block_content_edit);
        $out->add_block($block_node_edit);
    }
    
    
    public function act_remove() {
        $node_id = (int) tos_http_get('node_id');
        if ($node_id <= 0) tos_die();
        
        $node_type = trim($this->node_type);
        if (empty($node_type)) tos_die();
        
        $module_node = m('node');
        
        $parent_id = $module_node->remove_node($node_id);
        if ($parent_id !== false) {
            $this->remove_content($node_id);
            //$module_node->act_display($parent_id);
            tos_router_redirect(array('a' => 'display', 'm' => 'node', 'node_id' => $parent_id));
        }
        
    }
    
    
    public function act_display($node_id) {
        $template = $this->model('page')->get_template($node_id);
        return $this->view("page/{$template}");
    }
    
    
    abstract function edit_content($node_id);
    abstract function get_content($node_id);
    abstract function remove_content($node_id);
    
    abstract function block_display_content($node_id);
}
?>